﻿/****************************** Module Header ******************************\
 * Module Name:  ExMethodForPage.cs
 * Module Version:  1.0.0 (2011-9-30)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to extension webcontrol instance's method.  It supplies 
 * public static extension methods to load data or get othor control.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace UCF.Core.Web
{
    /// <summary>
    /// Page服务器控件扩展方法
    /// </summary>
    public static class ExMethodForPage
    {
        /// <summary>
        /// 获取指定层级的母版页对象
        /// </summary>
        /// <param name="page"></param>
        /// <param name="level"></param>
        /// <returns></returns>
        public static MasterPage GetParent(this Page page, int level)
        {
            MasterPage mpPage = new MasterPage();
            MasterPage mpTem = page.Master;
            if (level <= 1) return mpTem;
            int i = 1;
            while (i < level)
            {
                mpPage = mpTem;
                mpTem = mpTem.Master;
                if (mpTem == null) break;
            }
            return mpPage;
        }
        /// <summary>
        /// 获取最底层的母版页对象
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public static MasterPage GetBasicMaster(this Page page)
        {
            return GetParent(page, 99);
        }

        /// <summary>
        /// 获取母版页中的模板控件
        /// </summary>
        /// <param name="page"></param>
        /// <param name="controlID"></param>
        /// <param name="deep">层级</param>
        /// <returns></returns>
        public static ContentPlaceHolder GetContentPlaceHolder(this Page page, int deep = 1, string controlID = "ContentPlaceHolder1")
        {
            MasterPage m = page.Master;
            if (deep == 1) return ((ContentPlaceHolder)page.Master.FindControl(controlID));
            if (deep == 2) return ((ContentPlaceHolder)page.Master.Master.FindControl(controlID).FindControl(controlID));
            if (deep == 3) return ((ContentPlaceHolder)page.Master.Master.FindControl(controlID).FindControl(controlID).FindControl(controlID));
            return ((ContentPlaceHolder)page.Master.FindControl(controlID));
        }

        /// <summary>
        /// 注册头部CSS引用
        /// </summary>
        /// <param name="p"></param>
        /// <param name="key"></param>
        /// <param name="href"></param>
        public static void RegisterHeaderLink(this Page p, string key, string href)
        {
            bool hasItem = false;
            foreach (Control c in p.Header.Controls)
            {
                if (c.GetType() == typeof(HtmlLink) && ((HtmlLink)c).ID == key)
                {
                    hasItem = true;
                    break;
                }
            }
            if (hasItem) return;
            HtmlLink lnk = new HtmlLink();
            lnk.ID = key;
            lnk.Href = href;
            lnk.Attributes.Add("rel", "stylesheet");
            lnk.Attributes.Add("type", "text/css");
            p.Header.Controls.Add(lnk);
        }
    }
}
